//
//  CustomLoadingView.m
//  WisdomZhuHai
//
//  Created by yewei on 13-9-12.
//  Copyright (c) 2013年 wyd. All rights reserved.
//

#import "CustomLoadingView.h"
#import "ProgressView.h"

@interface CustomLoadingView()

@property (nonatomic,retain) NSString *timeOutMessage;
@property float timeoutSec;
@property (nonatomic,retain) ProgressView *loadingView;

-(void)showTimeoutAlert;

@end

@implementation CustomLoadingView

@synthesize timeOutMessage;
@synthesize loadingView;

CustomLoadingView *sharedCustomLoadingView = nil;

+ (CustomLoadingView*)shareInstance
{
    if (sharedCustomLoadingView == nil)
    {
        sharedCustomLoadingView = [[CustomLoadingView alloc] init];
    }
    return sharedCustomLoadingView;
}

- (id)init
{
    self = [super init];
    if (self) {
        self.timeOutMessage = nil;
        _timeoutSec = 30.0f;
    }
    
    return self;
}

-(void)dealloc
{
    [loadingView release];
    [timeOutMessage release];
    [super dealloc];
}

-(void)setLoadingTimeOut:(float)second timeOutMessage:(NSString *)str
{
    _timeoutSec = second;
    self.timeOutMessage = str;
}

-(void)showLoadingViewWithSuperView:(UIView *)superView message:(NSString *)str
{
    self.loadingView = [[[ProgressView alloc] initWithMessage:nil textColor:[UIColor whiteColor] frame:superView.bounds] autorelease];
    [superView addSubview: loadingView];
    
    if(_timeoutSec > 0.01)
    {
        [NSTimer scheduledTimerWithTimeInterval:_timeoutSec target:self selector:@selector(showTimeoutAlert) userInfo:nil repeats:NO];
    }
}

-(void)setRectHidden:(BOOL)hidden
{
    self.loadingView.roundRectHidden = hidden;
}
-(void)setWillCoverTouchEvent:(BOOL)bCover
{
    self.loadingView.bCoverTouchEvent = bCover;
}
-(void)stopLoadingView
{
    if(loadingView != nil)
    {
        [loadingView removeFromSuperview];
        self.loadingView = nil;
    }
    _timeoutSec = 0.0;
    self.timeOutMessage = nil;

}
-(void)showTimeoutAlert
{
    if([self.timeOutMessage length] > 0)
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:self.timeOutMessage delegate:nil cancelButtonTitle:NSLocalizedString(@"Confirm", nil) otherButtonTitles: nil];
        [alert show];
        [alert release];
    }
    [self stopLoadingView];
}


@end
